package com.gofunbuy.app.member;

import com.gofunbuy.R;
import com.gofunbuy.TabHomeGroup;
import com.gofunbuy.TabMemberGroup;
import com.gofunbuy.app.home.HomeActivity;
import com.gofunbuy.util.GoFunBuyUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TabMemberActivity extends Activity implements OnClickListener {
	private TextView tvTitle, tvLogin;
	private LinearLayout linearRegistion, linearLogin, linearFavorableDetail,
			linearTelCard, linearMember;
	private Context mContext;
	private String mUserName = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_layout);
		((TextView) findViewById(R.id.topbar_back))
				.setVisibility(View.INVISIBLE);
		mContext = this;
		tvTitle = (TextView) findViewById(R.id.topbar_title);
		tvTitle.setText(R.string.title_member);
		mUserName = GoFunBuyUtils.readUsernameFromFile(mContext);
		initLayout();
	}

	private void initLayout() {
		linearRegistion = (LinearLayout) findViewById(R.id.id_member_regist);
		linearLogin = (LinearLayout) findViewById(R.id.id_member_login);
		linearFavorableDetail = (LinearLayout) findViewById(R.id.id_member_favorable_detail);
		linearTelCard = (LinearLayout) findViewById(R.id.id_member_telcard);
		linearMember = (LinearLayout) findViewById(R.id.id_member_member_doc);
		linearRegistion.setOnClickListener(this);
		linearLogin.setOnClickListener(this);
		linearFavorableDetail.setOnClickListener(this);
		linearTelCard.setOnClickListener(this);
		linearMember.setOnClickListener(this);

		tvLogin = (TextView) findViewById(R.id.id_member_login_name);
		if (mUserName != null) {
			tvLogin.setText(R.string.login_out);
			linearRegistion.setVisibility(View.GONE);
		} else {
			tvLogin.setText(R.string.title_login);
			linearRegistion.setVisibility(View.VISIBLE);
		}

	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(TabMemberActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabMemberGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabMemberGroup.group.setContentView(view);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.id_member_regist:
			startActivity(SubMemberRegistActivity.class);
			break;
		case R.id.id_member_login:
			if (mUserName == null)// 登入
			{
				startActivity(SubMemberLoginActivity.class);
			} else// 登出
			{
				GoFunBuyUtils.clearUsernameAndPwd(mContext);
				Intent it = new Intent(this, SubMemberLoginActivity.class);
				it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(it);
				if (TabMemberGroup.group != null) {
					GoFunBuyUtils.mUserID = null;
					TabMemberGroup.group.finish();
				}
			}
			break;
		case R.id.id_member_favorable_detail:
			startActivity(SubMemberFavorableDetailActivity.class);
			break;
		case R.id.id_member_telcard:
			startActivity(SubMemberTelCardActivity.class);
			break;
		case R.id.id_member_member_doc:
			startActivity(SubMemberDocumentActivity.class);
			break;
		}
	}

}
